﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace BOMS.WebUI.Web.DataAnalyse
{
    public partial class groupHistory : System.Web.UI.Page
    {
        private BLL.S_BatSnapshot bll = new BLL.S_BatSnapshot();

        protected void Page_Load(object sender, EventArgs e)
        {
            drpEquipG.SelectedIndexChanged += new EventHandler(drp_SelectedIndexChanged);
            drpBatgrpG.SelectedIndexChanged += new EventHandler(drp_SelectedIndexChanged);
            btnQueryG.Click += new EventHandler(btnQueryG_Click);
            if (!IsPostBack)
            {
                BindDropdownlist(drpEquipG);
            }
        }
        private int BindData()
        {

            StringBuilder strWhere = new StringBuilder(128);

            strWhere.AppendFormat("Generation={0} and EquipId='{1}' and BatteryGroup='{2}'"
          , Common.GENRATION_NUM, drpEquipG.SelectedValue, drpBatgrpG.SelectedValue);

            string str1 = txtDtmBegin.Text.Trim();
            string str2 = txtDtmEnd.Text.Trim();
            DateTime t1, t2;
            if (str1 != "" && DateTime.TryParse(str1, out t1))
            {
                strWhere.AppendFormat(" and LogTime>='{0}'", str1);
            }

            if (str2 != "" && DateTime.TryParse(str2, out t2))
            {
                strWhere.AppendFormat(" and LogTime<='{0}'", str2);
            }

            BLL.S_BatgrpSubLog logbll = new BLL.S_BatgrpSubLog();


            int pageSize = PagerG.PageSize;
            int curPage = PagerG.CurrentPageIndex;
            int recordCount;

            List<Model.S_BatgrpSubLog> logList = logbll.GetModelListFromPage(strWhere.ToString(), "LogTime ASC", pageSize, curPage, out recordCount);
            this.PagerG.RecordCount = recordCount;

            Repeater1.DataSource = logList;
            Repeater1.DataBind();

            this.Label_begin.Text = this.txtDtmBegin.Text == "" ? "上线" : this.txtDtmBegin.Text;
            this.Label_end.Text = this.txtDtmEnd.Text == "" ? "今天" : this.txtDtmEnd.Text;
            this.Label_count.Text = recordCount.ToString();
            this.Label_wince.Text = this.drpEquipG.SelectedItem.Text;
            this.Label_group.Text = this.drpBatgrpG.SelectedItem.Text;

            Object[] objs = {drpEquipG.Text,
                drpBatgrpG.SelectedValue,
                txtDtmBegin.Text,
                txtDtmEnd.Text};


            Button2.PostBackUrl = string.Format("GroupHistoryPic.aspx?generation=" + Common.GENRATION_NUM + "&equipid={0}&batgrp={1}&dtmbegin={2}&dtmend={3}",
                objs
                );
            return recordCount;
        }
        protected void PagerG_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }
        void btnQueryG_Click(object sender, EventArgs e)
        {
            int n = BindData();
            if (n > 0)
            {
                Button2.Enabled = true;
                Button3.Enabled = true;
            }
        }
        void BindDropdownlist(object aim)
        {
            DropDownList drp = (DropDownList)aim;
            List<string> li = null;
            Dictionary<string, string> dict = null;
            if (drp == drpEquipG)
            {
                dict = bll.GetEquipList();
                foreach(KeyValuePair<string,string> kvp in dict)
                {
                    ListItem Item = new ListItem(kvp.Value, kvp.Key);
                    if (!drp.Items.Contains(Item))
                    {
                        drp.Items.Add(Item);
                    }
                }

            }
            else if (drp == drpBatgrpG)
            {
                li = bll.GetBatgrpList(drpEquipG.SelectedValue);
                //li.Add("0");//通信中断时，查不到电池组信息

                drp.Items.Clear();
                ListItem defaultItem = new ListItem("请选择电池组", "");
                drp.Items.Add(defaultItem);

                for (int i = 0; i < li.Count; i++)
                {
                    ListItem Item = new ListItem(li[i], li[i]);
                    if (!drp.Items.Contains(Item))
                    {
                        drp.Items.Add(Item);
                    }
                }
            }
            
           
        }
        void drp_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList drp = (DropDownList)sender;
            if (drp == drpEquipG)
            {
                BindDropdownlist(drpBatgrpG);
            }
        }

        private List<Model.S_BatgrpSubLog> getAllDataForExcel()
        {
            StringBuilder strWhere = new StringBuilder(128);

            strWhere.AppendFormat("Generation={0} and EquipId='{1}' and BatteryGroup='{2}'"
          , Common.GENRATION_NUM, drpEquipG.SelectedValue, drpBatgrpG.SelectedValue);

            string str1 = txtDtmBegin.Text.Trim();
            string str2 = txtDtmEnd.Text.Trim();
            DateTime t1, t2;
            if (str1 != "" && DateTime.TryParse(str1, out t1))
            {
                strWhere.AppendFormat(" and LogTime>='{0}'", str1);
            }

            if (str2 != "" && DateTime.TryParse(str2, out t2))
            {
                strWhere.AppendFormat(" and LogTime<='{0}'", str2);
            }

            BLL.S_BatgrpSubLog logbll = new BLL.S_BatgrpSubLog();


            int pageSize = 100000;
            int curPage = 1;
            int recordCount1;

            List<Model.S_BatgrpSubLog> logList = logbll.GetModelListFromPage(strWhere.ToString(), "LogTime ASC", pageSize, curPage, out recordCount1);
            this.PagerG.RecordCount = recordCount1;

            return logList;


        }

        private void gridviewAddColumn(GridView gv)
        {
            BoundField field;

            field = new BoundField();
            field.HeaderText = "记录号";
            field.DataField = "LogID";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "主机地址";
            field.DataField = "EquipID";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "电池组";
            field.DataField = "BatteryGroup";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "总电压(V)";
            field.DataField = "Vval";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "总电流(A)";
            field.DataField = "Ival";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "充放容量(Ah)";
            field.DataField = "CdFree";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "充放计时";
            field.DataField = "CdTime";
            gv.Columns.Add(field);


            field = new BoundField();
             field.HeaderText = "剩余容量(Ah)";
            field.DataField = "CapFree";
            gv.Columns.Add(field);

             field = new BoundField();
             field.HeaderText = "剩余可用时";
             field.DataField = "TimeFree";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "工作状态";
            field.DataField = "Status";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "故障内容";
            field.DataField = "WarningContentDescr";
            gv.Columns.Add(field);

            field = new BoundField();
            field.HeaderText = "时间";
            field.DataField = "LogTime"; ;
            gv.Columns.Add(field);

        }
        protected void Button3_Click(object sender, EventArgs e)
        {
           
            System.Web.UI.WebControls.GridView dgExport = new System.Web.UI.WebControls.GridView();

            //添加列
            gridviewAddColumn(dgExport);
            dgExport.DataSource = getAllDataForExcel();
            dgExport.AllowPaging = false;
            dgExport.AutoGenerateColumns = false;
            dgExport.DataBind();


            Response.Clear();
            Response.BufferOutput = true;
            //设定输出的字符集
            Response.Charset = "UTF-8";
            //假定导出的文件名为FileName.xls
            Response.AppendHeader("Content-Disposition", "attachment;filename=GroupHistory.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            //设置导出文件的格式
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            //关闭ViewState
            EnableViewState = false;
            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
            dgExport.RenderControl(textWriter);

            //把HTML写回浏览器
            Response.Write(stringWriter.ToString());
            Response.End();
        }
    }
}